using Xunit;

namespace GearUp.Crawler.Tests
{
    public class LinkManagerTests
    {
        private string baseUrl = "http://lorebook.lotro.com/wiki/";
        
        [Fact]
        public void ShouldMakeLinksFullyQualified()
        {
            var manager = new LinkManager(baseUrl);
            var actual = manager.FullyQualifyLink("http://lorebook.lotro.com/wiki/Category:Items", "#");
            Assert.Equal("http://lorebook.lotro.com/wiki/Category:Items#", actual);
        }

        [Fact]
        public void ShouldMakeRelativeLinksFullyQualified()
        {
            var manager = new LinkManager(baseUrl);
            var actual = manager.FullyQualifyLink("http://lorebook.lotro.com/wiki/fred/Category:Items", "../checkme.Html");
            Assert.Equal("http://lorebook.lotro.com/wiki/checkme.Html", actual);
        }

        [Fact]
        public void ShouldFullyQualifyBasePaths()
        {
            var manager = new LinkManager(baseUrl);
            var actual = manager.FullyQualifyLink("http://lorebook.lotro.com/wiki/Category:Items", "/wiki/Category:Armour");
            Assert.Equal("http://lorebook.lotro.com/wiki/Category:Armour", actual);
        }

        [Fact]
        public void ShouldLeaveFullyQualifiedLinksUnchanged()
        {
            var manager = new LinkManager(baseUrl);
            var actual = manager.FullyQualifyLink("http://lorebook.lotro.com/wiki/Category:Items", "http://lorebook.lotro.com/wiki/Category:Armour");
            Assert.Equal("http://lorebook.lotro.com/wiki/Category:Armour", actual);
        }

        [Fact]
        public void ShouldReturnEmptyForExternalLinks()
        {
            var manager = new LinkManager(baseUrl);
            var actual = manager.FullyQualifyLink("http://lorebook.lotro.com/wiki/Category:Items", "http://www.google.com");
            Assert.Equal(string.Empty, actual);
        }

        [Fact]
        public void ShouldReturnEmptyForLinkPathsAboveBaseUrl()
        {
            var manager = new LinkManager(baseUrl);
            var actual = manager.FullyQualifyLink("http://lorebook.lotro.com/wiki/Category:Items", "http://lorebook.lotro.com/home");
            Assert.Equal(string.Empty, actual);
        }

        [Fact]
        public void ShouldReturnEmptyForRelativePathsAboveBaseUrl()
        {
            var manager = new LinkManager(baseUrl);
            var actual = manager.FullyQualifyLink("http://lorebook.lotro.com/wiki/Category:Items", "../checkme.Html");
            Assert.Equal(string.Empty, actual);
        }

        [Fact]
        public void ShouldExtractUrlFromAnchorNodes()
        {
            var document = new LorebookDocument(Resource1.ArmourPageHtml);
            var anchorNode = document.EditLink();
            var actual = LinkManager.ExtractUrlFromNode(anchorNode);
            Assert.Equal("/index.php?title=Armour:Breastplate_of_the_Lady%27s_Courage&amp;action=edit", actual);
        }

        [Fact]
        public void ShouldReturnEmptyStringWhenTryingToExtractUrlFromNonAnchorTags()
        {
            var document = new LorebookDocument(Resource1.ArmourPageHtml);
            var itemDetails = document.OfficialLorebookEntry();
            var actual = LinkManager.ExtractUrlFromNode(itemDetails);
            Assert.Equal(string.Empty,actual);
        }

        [Fact]
        public void ShouldNotBlowUpWhenTryingToExtractUrlFromNullNode()
        {
            var actual = LinkManager.ExtractUrlFromNode(null);
            Assert.Equal(string.Empty, actual);
        }

    }
}